package array;
//数组线性查找
import java.util.Scanner;

public class LinearSearch {
    static int linearSearch(int[]a,int key){
        for (int i = 0; i <a.length ; i++) {
            if (a[i]==key)
                return i;//查找成功，返回索引

        }return -1;
    }




    public static void main(String[] args) {
        Scanner stdIn=new Scanner(System.in);
        //元素的个数
        //System.out.print("想要几个元素：");
        //int num=stdIn.nextInt();
        //new一个新数组
       // int[] x=new int[num];
        //赋值
       /* for (int i = 0; i <num ; i++) {
            System.out.print("x["+i+"]=");
            x[i]=stdIn.nextInt();

        }*/
        //System.out.println("输入要查找的值：");
        //int key=stdIn.nextInt();//要查找的值
        //int idx=linearSearch(x,key);
        int idx=linearSearch(new int[]{1,45,67,45,78},56);
        if (idx==-1)
            System.out.println("不存在该值的元素");
        else
            System.out.println("找到该元素"+"x["+idx+"]");
    }
}
